home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / human interface toolbox / labelmenu / setmenutitle.c < prev   
Encoding:
C/C++ Source or Header  |  2000-06-23  |  4.1 KB  |  126 lines

  1. /*
  2.     File:        SetMenuTitle.c
  3.  
  4.     Contains:    This demonstrates a program with a Finder-like label menu.
  5.                 Each label menu item has a 12x16 pixel 'cicn and the
  6.                 color and name of all the items are updated if the user
  7.                 changes anything in the "Labels" control panel.
  8.                 This also demonstrates how to change a menu tile to an icon.
  9.  
  10.     Written by: David Hayward    
  11.  
  12.     Copyright:    Copyright © 1993-1999 by Apple Computer, Inc., All Rights Reserved.
  13.  
  14.                 You may incorporate this Apple sample source code into your program(s) without
  15.                 restriction. This Apple sample source code has been provided "AS IS" and the
  16.                 responsibility for its operation is yours. You are not permitted to redistribute
  17.                 this Apple sample source code as "Apple sample source code" after having made
  18.                 changes. If you're going to re-distribute the source, we require that you make
  19.                 it clear in the source that the code was descended from Apple sample source
  20.                 code, but that you've made changes.
  21.  
  22.     Change History (most recent first):
  23.                 8/6/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  24.                 4/8/94                          updated project to use the new Universal Header
  25.                                               added routine to change a menu title
  26.                                             added abillity to change Label menu's title to a
  27.                                             small icon (for info on how this is done see IM:Text
  28.                                             page 7-39
  29.  
  30. */
  31.  
  32.  
  33. #include <menus.h>
  34. #include <types.h>
  35. #include <memory.h>
  36.  
  37.  
  38. void SetMenuTitle (MenuHandle theMenu, Str255 theNewTitle);
  39.  
  40.  
  41. void SetMenuTitle (MenuHandle theMenu, Str255 theNewTitle)
  42. {
  43.     Byte            oldTitleLength,        newTitleLength;
  44.     Ptr                oldItemLocation,    newItemLocation;
  45.     Size            oldHandleSize,        newHandleSize;
  46.     Ptr                theMenuAsAPtr;
  47.     Ptr                theDataAsAPtr;
  48.     Size            toMove;
  49.     short            difference;
  50.     char            theOldState;
  51.     
  52.     theOldState = HGetState((Handle)theMenu);    /* get the old menu handle state */
  53.     
  54.     HLock((Handle)theMenu);                        /* lock it */
  55.     theMenuAsAPtr = (Ptr)*theMenu;                /* offset to the menu data area is based on the */
  56.     theDataAsAPtr = (Ptr)&(**theMenu).menuData;    /* MenuInfo structure in Menus.h and Inside Mac */
  57.  
  58.     oldTitleLength = theDataAsAPtr[0];
  59.     newTitleLength = theNewTitle[0];
  60.     
  61.     difference = newTitleLength - oldTitleLength;
  62.     
  63.     oldHandleSize = GetHandleSize((Handle)theMenu);
  64.     newHandleSize = oldHandleSize + difference;
  65.  
  66.     /* If your titles are not all the same length, (i.e. difference != 0) */
  67.     /* then you are going to have to shift the items in the menu around, */
  68.     /* since the Menu Manager figures out where the items start based on string lengths */
  69.  
  70.         
  71.         if (difference > 0)
  72.         {
  73.             /* we have to grow the handle, so call SetHandleSize() then */
  74.             /* move the menu item date to make room for the longer title */
  75.  
  76.             HUnlock((Handle)theMenu);
  77.             SetHandleSize((Handle)theMenu, newHandleSize);
  78.  
  79.             /* check memerror here  */
  80.  
  81.             HLock((Handle)theMenu);
  82.             theMenuAsAPtr = (Ptr)*theMenu;
  83.             theDataAsAPtr = (Ptr)&(**theMenu).menuData;
  84.  
  85.             /* here, we get the location of the actual menu items */
  86.             /* by going past the header and the current title string */
  87.             
  88.             oldItemLocation = theDataAsAPtr + oldTitleLength + 1;
  89.             newItemLocation = theDataAsAPtr + newTitleLength + 1;
  90.             toMove = oldHandleSize - (oldItemLocation - theMenuAsAPtr);
  91.             BlockMove(oldItemLocation,newItemLocation,toMove);
  92.         }
  93.         
  94.         if (difference < 0)
  95.         {
  96.             /* we have to shrink the handle, so move the data first */
  97.             
  98.             oldItemLocation = theDataAsAPtr + oldTitleLength + 1;
  99.             newItemLocation = theDataAsAPtr + newTitleLength + 1;
  100.             toMove = oldHandleSize - (oldItemLocation - theMenuAsAPtr);
  101.             BlockMove(oldItemLocation,newItemLocation,toMove);
  102.             
  103.             /* now we can resize the menu handle */
  104.             
  105.             HUnlock((Handle)theMenu);
  106.             SetHandleSize((Handle)theMenu, newHandleSize);
  107.  
  108.             /* check memerror here  */
  109.  
  110.             HLock((Handle)theMenu);
  111.             theMenuAsAPtr = (Ptr)*theMenu;
  112.             theDataAsAPtr = (Ptr)&(**theMenu).menuData;;
  113.         }
  114.  
  115.     /* move the new title into place */
  116.     
  117.     BlockMove((Ptr)theNewTitle, theDataAsAPtr, newTitleLength+1);
  118.  
  119.     HSetState((Handle)theMenu,theOldState);        /* restore the original handle state */
  120.     
  121.     /* to insure redrawing, set and reset the menu bar  */
  122.  
  123.     SetMenuBar(GetMenuBar());
  124.     DrawMenuBar();
  125. }
  126.